/**
 * Created by cody on 2016/9/1.
 */
(function(window,document,$){

    function Common(){

    }


    Common.prototype.savePref = function(key,value){
        localStorage.setItem(key, value);
    }

    Common.prototype.getPref = function (key) {
        return localStorage.getItem(key);
    }

    /**
     * 浮点数只有一位
     * @private
     */
    Common.prototype.fixOne = function(f) {
        var str = f.toFixed(1);
        return parseFloat(str);
    }

    Common.prototype.isWeChat = function () {
        var userAgent = window.navigator.userAgent.toLowerCase();
        return /micromessenger/i.test(userAgent);
    }

    Common.prototype.copy = function(des,src){
        for(var key in src){
            if(src.hasOwnProperty(key)){
                des[key] = src[key];
            }
        }
    }

    Common.prototype.showLoading = function(text){
        var $floatUI = $('<div class="float-ui">'+
            '<div class="float-content"><span class="text"></span></div>'+
            '</div>');
        $floatUI.find('.text').text(text);
        $(document.body).append($floatUI);
        return $floatUI;
    }

    Common.prototype.dismissLoading=function ($floatUI) {
        $floatUI.remove();
    }
    /**
     * toast 默认2s
     * @param text
     * @param delay 默认3000，可以不填写
     */
    Common.prototype.toast=function(text,delay){
        var $toast = $('<div class="toast" style="position:fixed;z-index: 99999;width: 100%;padding:15%;bottom: 30%;text-align: center;" >'+
            '<span class="text" style="display: inline-block;padding:.5rem;color: #fff;background: rgba(0,0,0,.3);line-height: 1.3;"></span>'+
        '</div>');

        $toast.find('.text').text(text);
        var t = delay || 3000;
        $(document.body).append($toast);

        setTimeout(function(){
            $toast.remove();
        },t);
    }

    Common.prototype.parseHash = function(inHash){
        var hash = inHash || location.hash;
        var index = hash.lastIndexOf('#');
        var path = hash.substring(index + 1);

        var kvs = path.split('&');
        var data = {};
        for(var i = 0;i < kvs.length;i ++){
            var kv = kvs[i].split('=');
            var key = kv[0];
            var value = kv.length > 1 ? kv[1] : '';
            data[key] = value;
        }

        return data;
    }


    window.common = new Common();
}(window,document,$));